defmodule ProgContestWeb.ContestChannel do
  use ProgContestWeb, :channel

  alias ProgContest.{Contests, Evaluation}

  def join("contest:" <> _username, _payload, socket) do
    {:ok, Contests.get_current!(), socket}
  end

  def handle_in("submit", %{"quiz_id" => quiz_id, "language" => language, "code" => code}, socket) do
    :ok = Evaluation.evaluate_later({quiz_id, socket.assigns.username, language, code})
    {:reply, {:ok, %{}}, socket}
  end

  def handle_info("pass", payload, socket) do
    broadcast socket, "pass", payload
    {:noreply, socket}
  end

  def handle_info("fail", payload, socket) do
    broadcast socket, "fail", payload
    {:noreply, socket}
  end
end
